package com.zh.risk.aop;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zh.common.entity.AreaInterceptLog;
import com.zh.common.entity.IpInterceptLog;
import com.zh.common.entity.JSONResult;
import com.zh.risk.entity.CheckResult;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @version 1.0
 * @Description : 限制地区检测 切面
 * @Author : chenfei
 * @Create in : 2020/10/27 15:43
 */
@Aspect
@Component
public class AreaInterceptLogAop {

    @Autowired
    private MongoTemplate mongoTemplate;

    private static final Logger log = LoggerFactory.getLogger(AreaInterceptLogAop.class);


    @Pointcut("execution(* com.zh.risk.controller.CallRiskController.areaIntercept(..))")
    public void modRecordAopLog() { }

    private Long date = null;

    private String requestDATE =null;

    @Async
    @Before(value = "modRecordAopLog()")
    public void BeforeMod(@NotNull JoinPoint jp) {
        //设置请求时间
        date  = new Date().getTime();
        requestDATE = DateUtil.now();
    }


    @Async
    @AfterReturning(returning="result", value = "modRecordAopLog()")
    public void After(@NotNull  JoinPoint jp, JSONResult<Object> result) {
        // 获取方法信息
        MethodSignature methodSignature = (MethodSignature) jp.getSignature();
        Method method = methodSignature.getMethod();
        Long resultDate =new Date().getTime();
        try {
            ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
            JSONObject param = JSON.parseObject(String.valueOf(GetRequestParams.getRequestParamsByJoinPoint(jp).get("param")));
            AreaInterceptLog areaInterceptLog = new AreaInterceptLog();
            areaInterceptLog.setMethod(method.getName());
            areaInterceptLog.setNotes(apiOperation.notes());
            areaInterceptLog.setAreaCode(param.get("areaCode").toString());
            areaInterceptLog.setRequest(param.toString());
            areaInterceptLog.setRequestTime(requestDATE);
            CheckResult checkResult = (CheckResult) result.getData();
            areaInterceptLog.setResponseMsg(checkResult.getMessage());
            areaInterceptLog.setResponseStatus(checkResult.getFlag());
            areaInterceptLog.setResponseDate( (resultDate-date) +"ms" );
            areaInterceptLog.setLevelType(5);
            JSONObject response = new JSONObject();
            response.put("success",result.getSuccess());
            response.put("code",result.getCode());
            response.put("data",result.getData());
            areaInterceptLog.setResponse(response.toString());
            areaInterceptLog.setCreateDate(DateUtil.now());
            mongoTemplate.insert(areaInterceptLog);
        } catch (Exception e) {
            log.error("异常信息:{}", e.getLocalizedMessage());
        }
    }


}
